import 'package:flutter/cupertino.dart';

class DrawUnderLine extends StatefulWidget {
  final Widget child;

  final Color? color;

  final double strokeWidth;

  const DrawUnderLine({Key? key, required this.child, this.color, this.strokeWidth = 1})
      : super(key: key);

  @override
  State<DrawUnderLine> createState() => _DrawUnderLineState();
}

class _DrawUnderLineState extends State<DrawUnderLine> {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      foregroundPainter: DrawLineColorPainter(
          color: widget.color ?? const Color.fromRGBO(242, 242, 242, 1),
          strokeWidth: widget.strokeWidth),
      child: widget.child,
    );
  }
}

class DrawLineColorPainter extends CustomPainter {
  final Color color;

  final double strokeWidth;

  DrawLineColorPainter({required this.color, this.strokeWidth = 1});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..color = color
      ..strokeWidth = strokeWidth;
    canvas.drawLine(Offset(0, size.height), Offset(size.width, size.height), paint);
  }

  @override
  bool shouldRepaint(covariant DrawLineColorPainter oldDelegate) => this != oldDelegate;
}
